package com.xzq.erp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzq.erp.domain.dto.AfterSalesServiceDTO;
import com.xzq.erp.domain.dto.PageDTO;
import com.xzq.erp.domain.po.AfterSalesService;
import com.xzq.erp.domain.query.AfterSalesServicePageQuery;
import com.xzq.erp.domain.vo.AfterSalesServiceVO;
import com.xzq.erp.enums.AfterSalesServiceStatus;
import com.xzq.erp.mapper.AfterSalesServiceMapper;
import com.xzq.erp.service.IAfterSalesServiceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 售后服务表 服务实现类
 * </p>
 *
 * @author xzq
 * @since 2025-05-16
 */
@Service
@Slf4j
public class AfterSalesServiceServiceImpl extends ServiceImpl<AfterSalesServiceMapper, AfterSalesService> implements IAfterSalesServiceService {

    @Override
    public PageDTO<AfterSalesServiceVO> getAfterSalesServiceList(AfterSalesServicePageQuery query) {
        PageHelper.startPage(query.getCurrent(), query.getSize());
        // 注意Page继承了ArrayList，所以它是个集合对象
        Page<AfterSalesServiceVO> page = this.baseMapper.getAfterSalesServiceList(query);// 查询分页查询列表
        List<AfterSalesServiceVO> result = page.getResult();
        if(CollectionUtils.isEmpty(result)){
            return PageDTO.empty(page.getTotal(), (long)page.getPages());
        }
        log.info("查询结果:{},{}", page.getPages(), page.getTotal());
        return PageDTO.of(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AfterSalesServiceDTO afterSalesServiceDTO) {
        if (afterSalesServiceDTO.getId() == null) {
            throw new IllegalArgumentException("售后服务ID不能为空");
        }
        AfterSalesService service = getById(afterSalesServiceDTO.getId());
        if (service == null) {
            throw new IllegalArgumentException("售后服务不存在");
        }

        BeanUtil.copyProperties(afterSalesServiceDTO, service, "id", "createTime");
        service.setUpdateTime(LocalDateTime.now());
        updateById(service);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long add(AfterSalesServiceDTO afterSalesServiceDTO) {
        AfterSalesService service = BeanUtil.copyProperties(afterSalesServiceDTO, AfterSalesService.class);
        service.setCreateTime(LocalDateTime.now());
        service.setUpdateTime(LocalDateTime.now());
        // 新创建的售后服务默认为待处理状态
        service.setStatus(AfterSalesServiceStatus.PENDING);
        save(service);
        return service.getId();
    }

    @Override
    public AfterSalesServiceVO getDetailById(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("售后服务ID不能为空");
        }
        
        AfterSalesService service = getById(id);
        if (service == null) {
            return null;
        }
        
        // 使用Mapper关联查询获取带客户名称的详情信息
        AfterSalesServiceVO vo = this.baseMapper.getAfterSalesServiceDetail(id);
        if (vo == null) {
            // 如果关联查询为空，则使用基本转换
            vo = BeanUtil.copyProperties(service, AfterSalesServiceVO.class);
        }
        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStatus(Long id, Integer status) {
        if (id == null || status == null) {
            throw new IllegalArgumentException("参数不完整");
        }
        
        AfterSalesService service = getById(id);
        if (service == null) {
            throw new IllegalArgumentException("售后服务不存在");
        }
        
        AfterSalesServiceStatus newStatus = AfterSalesServiceStatus.of(status);
        if (newStatus == null) {
            throw new IllegalArgumentException("状态值无效");
        }
        
        service.setStatus(newStatus);
        service.setUpdateTime(LocalDateTime.now());
        updateById(service);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void submitSatisfactionScore(Long id, Integer score) {
        if (id == null || score == null) {
            throw new IllegalArgumentException("参数不完整");
        }
        
        if (score < 1 || score > 5) {
            throw new IllegalArgumentException("满意度评分必须在1-5分之间");
        }
        
        AfterSalesService service = getById(id);
        if (service == null) {
            throw new IllegalArgumentException("售后服务不存在");
        }
        
        service.setSatisfactionScore(score);
        service.setUpdateTime(LocalDateTime.now());
        updateById(service);
    }

    @Override
    public List<AfterSalesServiceVO> getAllServiceRecords() {
        return this.baseMapper.getAllServiceRecords();
    }
} 